/**
 * @description 通用的api接口
 */

import { axios, _formateAxiosData } from './axiosInstance'

/**
 * 获取是否存在用户
 * @method getIsExits
 * @param {String} userName
 * @returns {Object}
 */
async function getIsExits (userName) {
  let isExist = false
  // 发送POST请求
  try {
    let result = await axios.post('user/isExist', {
      userName
    })
    isExist = _formateAxiosData(result)
  } catch (e) {
    console.log(e)
  }
  return isExist
}

/**
 * 注册新用户
 * @method registerUser
 * @param {Object} data
 * @returns {Object}
 */
async function register (userInfo) {
  let result = await axios.post('user/register', userInfo)
  // 格式化数据返回
  return _formateAxiosData(result)
}
/**
 * 用户登录
 * @method login
 * @param {Object} userInfo 登陆的账户和密码
 * @returns {Object}
 */
async function login (userInfo) {
  // 发送请求
  let result = await axios.post('user/login', userInfo)
  // 格式化数据返回
  return _formateAxiosData(result)
}

/**
 * 获取用户信息
 * @method getUserInfo
 * @returns {Object}
 */
async function getUserInfo () {
  let userInfo = await axios.post('user/getUserInfo')
  return _formateAxiosData(userInfo)
}
/**
 * 改变用户信息
 * @method changeUserInfo
 * @param {Object} userInfo
 * @returns {Object}
 */
async function changeUserInfo (userInfo) {
  let result = await axios.patch('user/changeUserInfo', userInfo)
  return _formateAxiosData(result)
}

/**
 * 改变用户密码
 * @method changeUserPassword
 * @param { passwordInfo }
 * @returns {Object}
 */
async function changeUserPassword (passwordInfo) {
  let result = await axios.patch('user/changeUserPassword', passwordInfo)
  return _formateAxiosData(result)
}
/**
 * 退出登录
 * @method logout
 */
async function logout () {
  let result = await axios.post('user/logout')
  return _formateAxiosData(result)
}
export {
  getIsExits,
  register,
  login,
  getUserInfo,
  changeUserInfo,
  changeUserPassword,
  logout
}
